<template>
  <div>
    <div class="pa" :style="opacityStyle">
      <div class="top">
        <span class="jd jd-fanhui" @click="backFn"></span>
        商品列表
      </div>
    </div>
    <div class="fh1">
      <span class="jd jd-fanhui5" @click="backFn"></span>
    </div>
    <div class="TopBox">
      <div class="swipeBox">
        <van-swipe class="my-swipe" :autoplay="3000" @change="onChange">
          <van-swipe-item v-for="(item, index) in Obj.swiperImgs" :key="index"
            ><img :src="item" alt=""
          /></van-swipe-item>
          <template #indicator>
            <div class="custom-indicator">{{ current + 1 }}/{{ i }}</div>
          </template>
        </van-swipe>
      </div>
      <div class="title">
        <p class="red">￥{{ Obj.price }}</p>
        <p class="goodsName">{{ Obj.name }}</p>
      </div>
    </div>
    <div class="desc">
      <div class="descTitle">
        <span class="done">{{ Obj.isDirect ? "已选" : "未选" }}</span>
        <span class="descName">{{ Obj.name }}</span>
      </div>
      <div class="descConent">
        <ul>
          <li><span class="jd jd-zhengque"></span>可配送海外</li>
          <li><span class="jd jd-zhengque"></span>京东发货&售后</li>
          <li><span class="jd jd-zhengque"></span>京准宝</li>
          <li><span class="jd jd-zhengque"></span>211限时达</li>
          <li><span class="jd jd-zhengque"></span>可自提</li>
          <li><span class="jd jd-zhengque"></span>不可使用优惠卷</li>
        </ul>
      </div>
    </div>
    <div class="bgimg">
      <img
        v-for="(item, index) in Obj.detailImgs"
        :key="index"
        :src="item"
        alt=""
      />
    </div>
    <div class="bottom">
      <van-goods-action>
        <van-goods-action-icon icon="chat-o" text="客服" />
        <van-goods-action-icon icon="cart-o" text="购物车" />
        <van-goods-action-icon icon="shop-o" text="店铺" />
        <van-goods-action-icon icon="star" text="已收藏" color="#ff5000" />
        <van-goods-action-button
          type="warning"
          text="加入购物车"
          @click="onClickShopCar"
        />
        <van-goods-action-button
          type="danger"
          text="立即购买"
          @click="onClickBuy"
        />
      </van-goods-action>
    </div>
  </div>
</template>

<script>
import { getGoods } from "@/api/getGoods";
import token from "@/utils/token";
export default {
  data() {
    return {
      current: 0,
      Obj: {},
      i: null,
      Objlist: [],
      opacityStyle: { opacity: 0 },
    };
  },
  props: {},
  async created() {
    let id = this.$route.query.goodsId;
    // console.log(id);
    try {
      let res = await getGoods(token, id);
      this.Obj = res.data.data.goodsData;
      this.i = res.data.data.goodsData.swiperImgs.length;
      // console.log(this.Obj);
    } catch (error) {
      console.log(error);
    }
  },
  methods: {
    onClickLeft() {
      console.log("left");
    },
    onChange(index) {
      this.current = index;
    },
    backFn() {
      this.$router.back();
    },
    onClickShopCar() {
      // 先讲本地的list取出来赋值给Objlist，然后再push
      let res = localStorage.getItem("localGoodsData");
      if (res) {
        this.Objlist = JSON.parse(res);
      }
      this.Objlist.push(this.Obj);
      localStorage.setItem("localGoodsData", JSON.stringify(this.Objlist));
      this.$router.push({ path: "/shop" });
    },
    onClickBuy() {
      this.$router.push({ name: "buy", params: { obj: this.Obj } });
    },
    handleScroll() {
      const top = document.documentElement.scrollTop; //获取scroll偏移值
      if (top > 0 && top <= 300) {
        //实际按需求取范围
        const opacity = top / 300; //对opacity作计算，透明度从起始到1随偏移值而改变
        this.opacityStyle = { opacity }; //实时返回给opacityStyle
      }
    },
  },
  updated() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
};
</script>

<style scoped lang="less">
.pa {
  opacity: 0.5;
  padding-top: 20px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 99;
  background-color: #d81e06;
  .top {
    width: 750px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    font-size: 18px;
    color: #fff;
    font-weight: 800;
    position: relative;

    .jd-fanhui {
      position: absolute;
      left: 10px;
      top: 0px;
      font-size: 25px;
      font-weight: normal;
    }
  }
}
.fh1 {
  padding-top: 20px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 89;
  //   background-color: #d81e06;
  .jd-fanhui5 {
    position: absolute;
    left: 10px;
    top: 30px;
    font-size: 30px;
    font-weight: normal;
  }
}
.TopBox {
  .swipeBox {
    img {
      width: 100%;
      height: 100%;
    }
  }
  .title {
    width: 100%;
    padding: 8px;
    background-color: #fff;
    border-top: 1px solid #e5e5e5;
    .red {
      color: #d81e06;
      font-size: 22px;
    }
    .goodsName {
      color: #333;
      font-size: 18px;
    }
  }
}
.desc {
  width: 100%;
  //   height: 105px;
  padding: 9px;
  margin-top: 10px;
  background-color: #fff;
  .descTitle {
    width: 100%;
    height: 50px;
    border-bottom: 1px solid #e5e5e5;
    line-height: 50px;
    position: relative;
    .done {
      font-size: 16px;
      color: #999;
      display: inline-block;
      position: absolute;
      left: 0;
      top: 0px;
    }
    .descName {
      margin-left: 40px;
      color: #333;
      font-size: 18px;
      display: inline-block;
      white-space: nowrap;
      width: 80%;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
  .descConent {
    width: 100%;
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid #e5e5e5;
    ul {
      display: flex;
      li {
        font-size: 13px;
        color: #999;
        margin-left: 8px;
      }
    }
  }
}
.bgimg {
  padding-bottom: 56px;
  img {
    margin-top: -6px;
    width: 100%;
  }
}

.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  height: 365px;
  //   line-height: 365px;
  text-align: center;
  background-color: #39a9ed;
}
.custom-indicator {
  position: absolute;
  right: 5px;
  bottom: 5px;
  padding: 2px 5px;
  font-size: 12px;
  background: rgba(0, 0, 0, 0.1);
  color: #fff;
}

/deep/ .van-goods-action {
  height: 58px;
  .van-goods-action-icon {
    font-size: 16px;
    .van-goods-action-icon__icon {
      font-size: 24px;
    }
  }
  .van-goods-action-button {
    height: 50px;
    font-size: 20px;
  }
}
</style>
